﻿#region File and License Information
/*
<File>
	<License Type="BSD">
		Copyright © 2009 - 2012, Daniel Vaughan. All rights reserved.
	
		This file is part of Calcium (http://calciumsdk.net).

		Redistribution and use in source and binary forms, with or without
		modification, are permitted provided that the following conditions are met:
			* Redistributions of source code must retain the above copyright
			  notice, this list of conditions and the following disclaimer.
			* Redistributions in binary form must reproduce the above copyright
			  notice, this list of conditions and the following disclaimer in the
			  documentation and/or other materials provided with the distribution.
			* Neither the name of the <organization> nor the
			  names of its contributors may be used to endorse or promote products
			  derived from this software without specific prior written permission.

		THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
		ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
		WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
		DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
		DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
		(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
		LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
		ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
		(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
		SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
	</License>
	<Owner Name="Daniel Vaughan" Email="danielvaughan@outcoder.com" />
	<CreationDate>2009-03-29 23:31:47Z</CreationDate>
</File>
*/
#endregion

using System;
using System.Collections.Generic;

using DanielVaughan.InversionOfControl;

using Microsoft.Practices.ServiceLocation;

namespace DanielVaughan
{
	[Obsolete("Use Dependency instead.")]
	/// <summary>
	/// Helper class to resolve a Unity container
	/// that must be initialized once.
	/// </summary>
	public sealed class ServiceLocatorSingleton
	{
		#region Singleton implementation

		ServiceLocatorSingleton()
		{
		}

		public static ServiceLocatorSingleton Instance
		{
			get
			{
				return Nested.instance;
			}
		}

		class Nested
		{
			// Explicit static constructor to tell C# compiler
			// not to mark type as beforefieldinit
			static Nested()
			{
			}

			internal static readonly ServiceLocatorSingleton instance = new ServiceLocatorSingleton();
		}

		#endregion
 
		public object GetService(Type serviceType)
		{
			return ServiceLocator.Current.GetService(serviceType);
		}

		public object GetInstance(Type serviceType)
		{
			return ServiceLocator.Current.GetInstance(serviceType);
		}

		public object GetInstance(Type serviceType, string key)
		{
			return ServiceLocator.Current.GetInstance(serviceType, key);
		}

		public IEnumerable<object> GetAllInstances(Type serviceType)
		{
			return ServiceLocator.Current.GetAllInstances(serviceType);
		}

		public TService GetInstance<TService>()
		{
			return ServiceLocator.Current.GetInstance<TService>();
		}

		public bool TryGetInstance<TService>(out TService service)
		{
			var registrar = Instance.GetInstance<IDependencyRegistrar>();
			if (registrar.IsTypeRegistered(typeof(TService)))
			{
				service = ServiceLocator.Current.GetInstance<TService>();
				return true;
			}
			service = default(TService);
			return false;
		}

		public bool IsTypeRegistered<T>()
		{
			var registrar = Instance.GetInstance<IDependencyRegistrar>();
			return registrar.IsTypeRegistered(typeof(T));
		}

		public bool IsTypeRegistered(Type type)
		{
			var registrar = Instance.GetInstance<IDependencyRegistrar>();
			return registrar.IsTypeRegistered(type);
		}

		/// <summary>
		/// Tries to retrieve the service with the specified instance type.
		/// </summary>
		/// <param name="instanceType">Type of the instance.</param>
		/// <returns>The located instance, or <c>null</c> if not found.</returns>
		public object TryGetInstance(Type instanceType)
		{
			ArgumentValidator.AssertNotNull(instanceType, "instanceType");

			var registrar = Instance.GetInstance<IDependencyRegistrar>();
			if (registrar.IsTypeRegistered(instanceType))
			{
				var service = ServiceLocator.Current.GetInstance(instanceType);
				return service;
			}
			return null;
		}

		public TService GetInstance<TService, TDefaultImplementation>() 
			where TDefaultImplementation : TService
		{
			TService service;
			var registrar = Instance.GetInstance<IDependencyRegistrar>();
			if (registrar.IsTypeRegistered(typeof(TService)))
			{
				service = ServiceLocator.Current.GetInstance<TService>();
			}
			else
			{
				service = ServiceLocator.Current.GetInstance<TDefaultImplementation>();
			}
			return service;
		}

		public TService GetInstance<TService>(string key)
		{
			return ServiceLocator.Current.GetInstance<TService>(key);
		}

		public IEnumerable<TService> GetAllInstances<TService>()
		{
			return ServiceLocator.Current.GetAllInstances<TService>();
		}

#if USE_DEPRECATED_DEPENDCY_API
		public void RegisterInstance<TService>(TService service)
		{
			var registrar = Instance.GetInstance<IDependencyRegistrar>();
			registrar.RegisterInstance<TService>(service);
		}

		public void RegisterType<TFrom, TTo>() where TTo : TFrom
		{
			var registrar = Instance.GetInstance<IDependencyRegistrar>();
			registrar.RegisterType<TFrom, TTo>();
		}

		public void RegisterSingleton<TFrom, TTo>() where TTo : TFrom
		{
			var registrar = Instance.GetInstance<IDependencyRegistrar>();
			registrar.RegisterSingleton<TFrom, TTo>();
		}

		public void RegisterSingleton(Type fromType, Type toType)
		{
			var registrar = Instance.GetInstance<IDependencyRegistrar>();
			registrar.RegisterSingleton(fromType, toType);
		}
#else
		public void RegisterInstance<TService>(TService service)
		{
			var registrar = Instance.GetInstance<IDependencyRegistrar>();
			registrar.Register<TService>(service);
		}

		public void RegisterType<TFrom, TTo>() where TTo : TFrom
		{
			var registrar = Instance.GetInstance<IDependencyRegistrar>();
			registrar.Register<TFrom, TTo>();
		}

		public void RegisterSingleton<TFrom, TTo>() where TTo : TFrom
		{
			var registrar = Instance.GetInstance<IDependencyRegistrar>();
			registrar.Register<TFrom, TTo>(true);
		}

		public void RegisterSingleton(Type fromType, Type toType)
		{
			var registrar = Instance.GetInstance<IDependencyRegistrar>();
			registrar.Register(fromType, toType, true);
		}
#endif
	}
}
